Shader "Turbo/IVGI_TEST"
{
    Properties
    {
        _MainTex ("_MainTex", 2D) = "white" {}
        _Color ("_Color", Color) = (0,0,0,0)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "LightMode"="UniversalForward" }
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "../Shader/IVGI.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct Varings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 positionWS : TEXCOORD1;
                float3 normalWS : TEXCOORD2;
                float3 viewDirWS : TEXCOORD3;
                float4 IVGIColor : TEXCOORD4;

            };

            #pragma multi_compile _ _USE_IVGI _USE_IVGI_2D _USE_IVGI_VERTEX

            //#define _USE_IVGI
            
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            float4 _MainTex_ST;
            float3 _CenterPosition;
            int _DataIndex;
            bool _OnlyColor;
            float4 _Color;
            
            Varings vert (Attributes IN)
            {
                Varings OUT;
                
                VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
                VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);
                OUT.positionCS = TransformObjectToHClip(IN.positionOS);
                OUT.positionCS = positionInputs.positionCS;
                OUT.positionWS =  positionInputs.positionWS; 
                OUT.viewDirWS = GetCameraPositionWS() - positionInputs.positionWS;
                OUT.normalWS = normalInputs.normalWS;
                OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                
                #ifdef _USE_IVGI_VERTEX
                OUT.IVGIColor = SampleIVGI(OUT.positionWS, OUT.normalWS);                
                #endif
                
                return OUT;
            }
            
            float4 frag (Varings IN) : SV_Target
            {
                Light light = GetMainLight();
                float4 final = _Color;

                #ifdef _USE_IVGI
                final = SampleIVGI(IN.positionWS, IN.normalWS);
                //final = SampleIVGI(float3(19,10,10), IN.normalWS);l
                #endif

                #ifdef _USE_IVGI_2D
                final = SampleIVGI_2D(IN.positionWS, IN.normalWS);
                
                //final = SampleIVGI_2D(float3(19,10,10), IN.normalWS);
                #endif
                
                #ifdef _USE_IVGI_VERTEX
                final = IN.IVGIColor;
                #endif
                
                half3 lightDiffuseColor = LightingLambert(light.color * light.distanceAttenuation, light.direction, IN.normalWS);
                return float4(final.rgb + lightDiffuseColor * final.a, 1); // 
            }
            ENDHLSL
        }

        Pass
        {
            Name "ShadowCaster"
            Tags
            {
                "LightMode" = "ShadowCaster"
            }

            // -------------------------------------
            // Render State Commands
            ZWrite On
            ZTest LEqual
            ColorMask 0
            Cull[_Cull]

            HLSLPROGRAM
            #pragma target 2.0

            // -------------------------------------
            // Shader Stages
            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature_local _ALPHATEST_ON
            #pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA

            // -------------------------------------
            // Unity defined keywords
            #pragma multi_compile_fragment _ LOD_FADE_CROSSFADE

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing
            #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"

            // This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
            #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW

            // -------------------------------------
            // Includes
            #include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
            ENDHLSL
        }
    }
}
